package com.bosssoft.ecds.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bosssoft.ecds.dao.UabAgenBillMapper;
import com.bosssoft.ecds.entity.dto.AutoApplyDTO;
import com.bosssoft.ecds.entity.dto.AutoApplyReceiveDTO;
import com.bosssoft.ecds.entity.dto.DestroyDTO;
import com.bosssoft.ecds.entity.dto.UabAgenBillDTO;
import com.bosssoft.ecds.entity.po.UabAgenBillPO;
import com.bosssoft.ecds.service.AbstractService;
import com.bosssoft.ecds.service.RemotePoolService;
import com.bosssoft.ecds.service.UabAgenBillService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;

/**
 * @author 张东海
 * @date 2020/8/10
 * @description
 */
@Service
public class UabAgenBillServiceImpl extends AbstractService<UabAgenBillPO, UabAgenBillMapper>
    implements UabAgenBillService {

    @Resource
    MyServiceImpl myService;

    @Resource
    RemotePoolService remotePoolService;

    /**
     * 更新票据信息，在更新前从数据库读出版本号使乐观锁生效
     * @param bean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UabAgenBillPO bean) {
        UabAgenBillPO selectedBean = mapper.selectById(bean.getfId());
        super.update(selectedBean);
    }

    /**
     * 开票功能，从数据库读出票号最小的一条可用票据，
     * 票号不满十位的将被填充零至满十位，
     * 开票后进行检测，当可用票据低于十张时，自动向
     * 票据池申领十张新票据的票号段，随后据此将票据
     * 批量插入单位票据表中。
     * @return
     */
    public UabAgenBillDTO kai() {
        UabAgenBillPO billPo = mapper.selectForKai();
        UabAgenBillDTO billDto = new UabAgenBillDTO();
        billDto.setFType(billPo.getfBillName());
        billDto.setFBillNo(String.format("%010d",billPo.getfBillId()));
        billDto.setFBIllId(billPo.getfBillPrecode().toString());
        billPo.setFHxStatus(1);
        mapper.updateById(billPo);
        Integer counts = mapper.selectBillCounts();
        if(counts < 10){
            AutoApplyDTO applyDto = new AutoApplyDTO();
            applyDto.setBillTypeCode("01160201");
            applyDto.setNumber(10);
            AutoApplyReceiveDTO autoApplyReceiveDTO = remotePoolService.autoApply(applyDto);
            saveBatch(autoApplyReceiveDTO.getBillCodeBegin(),autoApplyReceiveDTO.getBillCodeEnd());
        }
        return billDto;
    }

    /**
     * 根据指定单位名称、票据8位编码、仓库ID与票号段，销毁票据
     * @param destroyDto
     * @return
     */
    public int destroyStockBill(DestroyDTO destroyDto){
        UpdateWrapper<UabAgenBillPO> wrapper = new UpdateWrapper<>();
        wrapper.eq("f_agen_id_code",destroyDto.getfAgenIdCode());
        wrapper.eq("f_bill_precode",destroyDto.getfBillBatchCode());
        wrapper.eq("f_warehouse_id",destroyDto.getfWarehouseId());
        wrapper.between("f_bill_id",destroyDto.getfBillNo1(),destroyDto.getfBillNo2());
        return mapper.delete(wrapper);
    }

    /**
     * 生成指定票号段内的票号，将其批量插入进单位票据表中
     * @param no1 起始票号
     * @param no2 终止票号
     */
    public void saveBatch(Integer no1,Integer no2){
        ArrayList<UabAgenBillPO> list = new ArrayList<>();
        for (Integer i = no1; i <no2 ; i++) {
            UabAgenBillPO billPo = new UabAgenBillPO();
            billPo.setFBillId(new BigInteger(i.toString()));
            list.add(billPo);
        }
        myService.saveBatch(list);
    }
}
